using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using CoachingOrganizer.Logic.Models.Tools.MoodLevel;
using MonoTouch.CoreGraphics;
using System.Drawing;

namespace CoachingOrganizer.iOS.Controllers.Tools.MoodLevel
{
    public partial class MoodLevelChartDetailsViewController : UIViewController
    {
        private readonly MoodLevelModel _model;
        private bool _valuesAreChanged;

        public MoodLevelChartDetailsViewController(IntPtr handle)
            : base(handle)
        {
            _model = ((MoodLevelDetailsViewController)ParentViewController).Model;
            _model.ValuesCollectionChanged += Model_ValuesCollectionChanged;
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            ChartView.Image = GetChart();
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            RemoveCurrentChart();
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
            if (_valuesAreChanged)
            {
                _valuesAreChanged = false;
                RemoveCurrentChart();
                ChartView.Image = GetChart();
            }
        }

        private UIImage GetChart()
        {
            UIImage outResult;
            MoodValueModel[] moodValues = _model.Values.ToArray();
            UIGraphics.BeginImageContext(ChartView.Bounds.Size);
            using (CGContext drawingContext = UIGraphics.GetCurrentContext())
            {
                drawingContext.SetFillColor(UIColor.Black.CGColor);
                drawingContext.FillRect(drawingContext.GetClipBoundingBox());
                float horizontalOffset = (float)drawingContext.GetClipBoundingBox().Width / (float)moodValues.Length;
                float verticalScale = (float)drawingContext.GetClipBoundingBox().Height / 10f;
                float verticalBaseOffset = (float)drawingContext.GetClipBoundingBox().Height / 2f;
                UIColor.Red.SetStroke();
                int i = 0;
                for (float x = 0f; x < drawingContext.GetClipBoundingBox().Width && i < moodValues.Length - 1; x += horizontalOffset, i++)
                    using (CGPath path = new CGPath())
                    {
                        path.AddLines(new PointF[] { new PointF(x, verticalBaseOffset - (float)moodValues[i].Value * verticalScale), new PointF(x + horizontalOffset, verticalBaseOffset - (float)moodValues[i + 1].Value * verticalScale) });
                        drawingContext.AddPath(path);
                        drawingContext.DrawPath(CGPathDrawingMode.Stroke);
                    }
                outResult = UIGraphics.GetImageFromCurrentImageContext();
            }
            UIGraphics.EndImageContext();
            return outResult;
        }

        private void RemoveCurrentChart()
        {
            UIImage chartImage = ChartView.Image;
            if (chartImage != null)
                chartImage.Dispose();
        }

        private void Model_ValuesCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _valuesAreChanged = true;
        }
    }
}